﻿#pragma once
#include "yzrutil.h"
#include "io/InputStream.h"

namespace yzrilyzr_util{
	ECLASS(RIFFReader, public yzrilyzr_io::InputStream){
	private:
	RIFFReader * root;
	yzrilyzr_lang::String fourcc;
	u_index ckSize;
	yzrilyzr_io::InputStream * stream;
	u_index filepointer=0;
	yzrilyzr_lang::String riff_type;
	u_index avail=0xffffffffL; // MAX_UNSIGNED_INT
	std::shared_ptr<RIFFReader> lastiterator;
	public:
	RIFFReader(yzrilyzr_io::InputStream * stream);
	int32_t read() override;
	u_index read(yzrilyzr_array::ByteArray & b, u_index offset, u_index len) override;
	u_index skip(u_index n) override;
	u_index available() override;
	void close() override;
	void finish();
	void readFully(yzrilyzr_array::ByteArray & b, u_index off, u_index len);
	// Read 32 bit unsigned integer from stream
	uint32_t readUnsignedInt();
	yzrilyzr_lang::String getFormat();
	void readFully(yzrilyzr_array::ByteArray & b);
	int64_t getFilePointer();
	bool hasNextChunk();
	std::shared_ptr<RIFFReader> nextChunk();
	std::shared_ptr<RIFFReader> innerChunk();
	yzrilyzr_lang::String getType();
	u_index getSize();
	// Read ASCII chars from stream
	yzrilyzr_lang::String readString(u_index len);
	// Read 8 bit signed integer from stream
	int8_t readByte();
	// Read 16 bit signed integer from stream
	int16_t readShort();
	// Read 32 bit signed integer from stream
	int32_t readInt();
	// Read 64 bit signed integer from stream
	int64_t readLong();
	// Read 8 bit unsigned integer from stream
	uint8_t readUnsignedByte();
	// Read 16 bit unsigned integer from stream
	uint16_t readUnsignedShort();
	};
}